package com.fuze.questionnaire.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fuze.questionnaire.common.Advice;
import com.fuze.questionnaire.common.CustomException;
import com.fuze.questionnaire.domain.dto.LoginInfoDTO;
import com.fuze.questionnaire.domain.entity.BasicUserInformation;
import com.fuze.questionnaire.mapper.BasicUserInformationMapper;
import com.fuze.questionnaire.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.WeakHashMap;

/**
 * @author sepc_
 * @date 10/20/2021 15:11
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    @Value("${wx.auth.code2Session}")
    String code2Session;

    @Value("${wx.auth.appid}")
    String appId;

    @Value("${wx.auth.secret}")
    String secret;

    final BasicUserInformationMapper basicUserInformationMapper;

    @Override
    public Optional<String> login(String code) {
        String url = code2Session +
                "appid=" + appId +
                "&secret=" + secret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        String res = HttpUtil.get(url);
        LoginInfoDTO infoDTO = JSONUtil.toBean(res, LoginInfoDTO.class);
        if (infoDTO == null) {
            throw new CustomException(Advice.FAILED_LOGIN);
        }
        return Optional.of(infoDTO.getOpenid());
    }

    @Override
    public Integer insertOrUpdate(BasicUserInformation userInformation) {
        QueryWrapper<BasicUserInformation> qw = new QueryWrapper<>();
        qw.eq(BasicUserInformation.COL_OPENID, userInformation.getOpenid());
        BasicUserInformation one = basicUserInformationMapper.selectOne(qw);
        if (one != null) {
            userInformation.setId(one.getId());
            if (basicUserInformationMapper.updateById(userInformation) != 1) {
                throw new CustomException(Advice.FAILED_UPDATE);
            }
            return userInformation.getId();
        }
        if (basicUserInformationMapper.insert(userInformation) != 1) {
            throw new CustomException(Advice.FAILED_INSERT);
        }
        return userInformation.getId();
    }

    @Override
    public boolean checkUserExist(String openid) {
        QueryWrapper<BasicUserInformation> qw = new QueryWrapper<>();
        qw.eq(BasicUserInformation.COL_OPENID, openid);
        BasicUserInformation one = basicUserInformationMapper.selectOne(qw);
        return one != null;
    }

    @Override
    public Integer getUserId(String openid) {
        BasicUserInformation one = new LambdaQueryChainWrapper<>(basicUserInformationMapper)
                .eq(BasicUserInformation::getOpenid, openid)
                .one();
        return one != null ? one.getId() : null;
    }

    @Override
    public BasicUserInformation listUserInfo(String openid) {
        return new LambdaQueryChainWrapper<>(basicUserInformationMapper)
                .eq(BasicUserInformation::getOpenid, openid)
                .one();
    }
}
